﻿using ProjectSemIII.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ProjectSemIII.Administrator.Process
{
    public class FAQSupportProcess
    {
        private project_sem3Entities projectEntity;
        public FAQSupportProcess()
        {
            projectEntity = new project_sem3Entities();
        }

        //Create new FAQ function
        public int createNewFAQ(FAQ faq)
        {
            int result = 0;
            try
            {
                projectEntity.FAQs.Add(faq);
                projectEntity.SaveChanges();
                result = 10;
            }
            catch
            {
                result = 100;
            }
            return result;
        }

        //Get all FAQ to list function
        public List<FAQ> getAllFAQ()
        {
            List<FAQ> listFAQ;
            try
            {
                listFAQ = (from faq in projectEntity.FAQs
                           select faq).ToList();
            }
            catch
            {
                listFAQ = new List<FAQ>();
            }
            return listFAQ;
        }

        //Check question is exits
        public bool checkQuestionTitle(int catalogID, string question)
        {
            bool flag = false;
            List<FAQ> listFAQ;
            listFAQ = (from faq in projectEntity.FAQs
                       where faq.FAQCatalog == catalogID
                       select faq).ToList();
            foreach (FAQ fa in listFAQ)
            {
                if (fa.Question.ToLower() == question.ToLower())
                {
                    flag = true;
                }
            }
            return flag;
        }

        //Delete FAQ by FAQ ID
        public int deleteFAQByFAQID(int FAQID)
        {
            try
            {
                var faq = (from f in projectEntity.FAQs
                           where f.FAQID == FAQID
                           select f).Single();
                projectEntity.FAQs.Remove(faq);
                projectEntity.SaveChanges();
                return 10;
            }
            catch
            {
                return 100;
            }
        }

        //Get info FAQ by FAQ ID
        public FAQ getInfoFAQ(int FAQID)
        {
            FAQ faq;
            try
            {
                faq = (from f in projectEntity.FAQs
                       where f.FAQID == FAQID
                       select f).Single();
            }
            catch
            {
                faq = new FAQ();
            }
            return faq;
        }

        //Update FAQ by FAQ ID
        public int updateFAQ(FAQ faq)
        {
            try
            {
                var faqInfo = (from f in projectEntity.FAQs
                               where f.FAQID == faq.FAQID
                               select f).Single();
                faqInfo.Question = faq.Question;
                faqInfo.FAQCatalog = faq.FAQCatalog;
                faqInfo.Answer = faq.Answer;
                projectEntity.SaveChanges();
                return 10;
            }
            catch
            {
                return 100;
            }
        }

        //Get support infomation function
        public Entity.Support getSupportInfo()
        {
            Entity.Support support;
            try
            {
                support = (from s in projectEntity.Supports
                           select s).First();
            }
            catch
            {
                support = new Entity.Support();
            }
            return support;
        }

        //Update support infomation function

        public int updateSupportInfo(Entity.Support support)
        {
            int result = 0;
            try
            {
                Entity.Support su = (from s in projectEntity.Supports
                                     where s.SupportID == support.SupportID
                                     select s).First();
                su.SupportName = support.SupportName;
                su.SupportPhone = support.SupportPhone;
                su.SupportAddress = support.SupportAddress;
                su.SupportEmail = support.SupportEmail;
                projectEntity.SaveChanges();
                result = 10;
            }
            catch
            {
                result = 100;
            }
            return result;
        }
    }
}